#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
int S[256];
string key;

int generateKey(){
	static int i = 0;
	static int j = 0;
	
	i = (i + 1) % 256;
	j = (j + S[i]) % 256;
	int temp = S[i];
	S[i] = S[j];
	S[j] = temp;
	return S[((S[i] + S[j])%256)];
}

int main(){
	key = "Key";
	for(int i=0; i<256; i++){
		S[i] = i;
	}
	int j=0, temp;
	int keylength = key.length();
	for(int i=0; i<256; i++){
		j = (j + S[i] + key[i%keylength])%256;
		/* swap S[i] and S[j]*/
		temp = S[i];
		S[i] = S[j];
		S[j] = temp;
	}
	string input;
	cout<<"Enter plaintext: (The hard-coded key currently is:\'"<<key<<"\')\t";
	cin>>input;
	int inlen = input.length();
	for(int i=0; i<inlen; i++){
		int key = generateKey();
		//printf("%x\t", key);
		int ch = input[i];
		int xo = key^ch;
		printf("%x", xo);
	}
	cout<<endl;
	return 0;
}
